---
id: option
title: Option
---

import Option, { toc as optiontoc } from "./generated/_option.mdx"

Belt (ReScript) represents the existence and nonexistence of a value by wrapping it with the `Option` type. TS Belt does the same and provides utility-functions for convenient work with the `Option` type.

```ts
type Option<T> = T | undefined | null
```

:::warning

Adding `noUncheckedIndexedAccess` to your `tsconfig.json` is mandatory for the `Option` type!

:::

```jsx live
function() {
  // ⬇️ remove all elements in the array below in order to see the default value
  const xs = ['hello', 'world', 'ts', 'belt']
  const value = pipe(
    O.fromNullable(xs), // → Some(['hello', 'world', 'ts', 'belt'])
    O.flatMap(A.dropExactly(2)), // → Some(['ts', 'belt'])
    O.map(A.join('-')), // → Some('ts-belt')
    O.getWithDefault('default value'), // returns `default value` if `None`
  )

  return value
}
```

<Option />

export const toc = optiontoc
